package com.wangzhixuan.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wangzhixuan.commons.utils.PageInfo;
import com.wangzhixuan.mapper.DescribeMapper;
import com.wangzhixuan.mapper.ImageMapper;
import com.wangzhixuan.model.Describe;
import com.wangzhixuan.model.Image;
import com.wangzhixuan.model.vo.DescribeVo;
import com.wangzhixuan.service.IDescribeService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhixuan.wang
 * @since 2017-07-30
 */
@Service
public class DescribeServiceImpl extends ServiceImpl<DescribeMapper, Describe> implements IDescribeService {
	
	@Autowired
    private DescribeMapper describeMapper;
	
	@Autowired
    private ImageMapper imageMapper;
    
	@Override
    public void selectDataGrid(PageInfo pageInfo) {
    	Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getSize());
        page.setOrderByField(pageInfo.getSort());
        page.setAsc(pageInfo.getOrder().equalsIgnoreCase("asc"));
        List<Describe> list = describeMapper.selectDescribePage(page, pageInfo.getCondition());
        pageInfo.setRows(list);
        pageInfo.setTotal(page.getTotal());
    }

	@Override
	public void insertDescribe(DescribeVo describeVo) {
		Date date = new Date();
		Describe describe = new Describe();
		describe.setCreateId(describeVo.getUserId());
		describe.setWorkId(describeVo.getWorkId());
		describe.setNotes(describeVo.getDesDescrib());
		describe.setCreateTime(date);
		describeMapper.insert(describe);
		Image image = null;
		for(String str : describeVo.getDesPictureArrList()){
			image = new Image();
			image.setCreateId(describeVo.getUserId());
			image.setWorkId(describeVo.getWorkId());
			image.setImgPath(str);
			image.setType("3");
			image.setCreateTime(date);
			image.setDescId(describe.getId());
			imageMapper.insert(image);
		}
	}

	@Override
	public List<Map<String, Object>> selectDescribeList(Integer workId) {
		List<Map<String,Object>> list = describeMapper.selectListbyWorkId(workId);
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("workId", workId);
		for(Map<String,Object> map : list){
			paramMap.put("descId", map.get("descId").toString());
			List<String> imgList = imageMapper.selectByDesIdAndWorkId(paramMap);
			map.put("desPictureArr", imgList);
			map.remove("descId");
		}
		return list;
	}
}
